<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item label="">
          <el-input
            v-model="listQuery.name"
            placeholder="请输入客户姓名/微信ID"
            clearable
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="listQuery.employee_name"
            placeholder="请输入员工姓名/微信ID"
            clearable
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-date-picker
            v-model="listQuery.search_date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click.native="handleFilter"
            >查询</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleBatchDelete"
            >批量删除</el-button
          >
        </el-form-item>
      </el-form>

      <el-table
        :data="datas"
        border
        v-loading="listLoading"
        fit
        highlight-current-row
        style="width: 100%"
        size="small"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          align="center"
          width="40"
        ></el-table-column>
        <el-table-column
          v-for="column in tableColumns"
          :key="column.key"
          :label="column.title"
          :prop="column.key"
          :align="column.align"
          :width="column.width ? column.width : ''"
        ></el-table-column>
        <el-table-column align="center" label="操作" width="90px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="handleEdit(scope)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getPointConsumptionList"
      />
    </el-card>
    <!-- 对话框表单 -->
    <el-dialog :visible.sync="dialogVisible" :title="getTitle" width="55%">
      <el-form
        :model="form"
        :rules="formRules"
        ref="formRef"
        label-width="120px"
        label-position="right"
        style="width: 95%; padding: 0 30px"
        status-icon
        size="small"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户姓名" prop="name">
              <el-input
                v-model="form.name"
                placeholder="客户姓名"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户微信ID" prop="openid">
              <el-input v-model="form.openid" placeholder="客户微信ID" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="商家微信ID" prop="employee_openid">
              <el-input type="text" v-model="form.employee_openid" placeholder="商家微信ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工姓名" prop="employee_name">
              <el-input v-model="form.employee_name" placeholder="员工姓名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="消费积分" prop="score">
              <el-input v-model="form.score" placeholder="消费积分"> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="剩余积分" prop="total_score">
              <el-input
                v-model="form.total_score"
                placeholder="剩余积分"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="时间" prop="update_time">
              <el-input v-model="form.update_time" placeholder="时间">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button type="danger" @click="dialogVisible = false" size="mini"
          >关闭</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  getPointConsumptions,
  delPointConsumptions,
} from "@/api/member";

import Pagination from "@/components/Pagination";

const defaultForm = {
  id: null,
  openid: null,
  name: null,
  employee_openid: null,
  employee_name: null,
  score: null,
  total_score: null,
};

export default {
  name: "ConsumePoint",
  data() {
    return {
      form: Object.assign({}, defaultForm),
      formRules: {},

      title: "积分消费",
      dialogVisible: false,
      dialogType: "new",
      datas: [],
      selections: [],
 
      tableColumns: [
        {
          key: "name",
          title: "客户姓名",
          align: "center",
          width: 120,
        },
        {
          key: "openid",
          title: "客户微信ID",
          align: "center",
        },
        {
          key: "employee_name",
          title: "员工姓名",
          align: "center",
        },
        {
          key: "employee_openid",
          title: "商家微信ID",
          align: "center",
        },
        {
          key: "score",
          title: "消费积分",
          align: "center",
          width: 120,
        },
        {
          key: "total_score",
          title: "剩余积分",
          align: "center",
          width: 120,
        },
        {
          key: "update_time",
          title: "时间",
          align: "center",
          width: 140,
        },
      ],

      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 15,
        name: null,
        employee_name: null,
        search_date: [],
      },
    };
  },
  computed: {
    ...mapGetters(["uid"]),
    getTitle() {
      return this.dialogType == "new"
        ? `新增${this.title}`
        : `查看${this.title}`;
    },
  },
  components: {
    Pagination,
  },
  created() {
    this.getPointConsumptionList();
  },
  methods: {

    getPointConsumptionList() {
      this.listLoading = true;
      getPointConsumptions(this.listQuery)
        .then((res) => {
          this.datas = res.results;
          this.listLoading = false;
          this.total = res.count;
        })
        .catch((error) => {
          console.log(error);
          this.$message.error("获取数据失败!");
          this.listLoading = false;
        });
    },
    handleSelectionChange(val) {
      this.selections = val.map((item) => item.id);
    },
    handleBatchDelete() {
      if (this.selections.length === 0) {
        this.$message.warning("请选择要删除的数据");
        return;
      }
      this.$confirm("确定要删除选中记录吗?", "删除提示：", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const sel_ids = {
            select_ids: this.selections,
          };
          const res = await delPointConsumptions(sel_ids);
          this.getPointConsumptionList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1;
      this.getPointConsumptionList();
    },

    // 修改
    handleEdit(scope) {
      this.form = scope.row;
      this.dialogType = "edit";

      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.formRef.clearValidate();
      });
    },
  },
};
</script>

<style lang='scss' scoped>
</style>